package com.lin.servlet.user;

import com.lin.pojo.User;
import com.lin.service.user.UserServiceImpl;
import com.lin.util.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("userCode");
        String pwd = req.getParameter("userPassword");
        UserServiceImpl userService = new UserServiceImpl();
        User info = userService.getUserInfo(name, pwd);
        if (null !=  info){
            User session_name = (User)req.getSession().getAttribute(Constant.SESSION_NAME);
            if (null == session_name){
                req.getSession().setAttribute(Constant.SESSION_NAME,info);
            }
            resp.sendRedirect("/smbms/jsp/frame.jsp");
        }else{
            req.setAttribute("message","用户名或者密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
